//
//  DetailViewController.m
//  Weibo
//
//  Created by taoshanchang on 15/2/1.
//  Copyright (c) 2015年 taoshanchang. All rights reserved.
//

#import "DetailViewController.h"
#import "CONSTS.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "WBHttpRequest.h"
#import "CommentModel.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _initView];
    self.title = @"微博正文";
    [self loadData];
}

-(void)_initView{
    _tableView = [[CommentTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-20) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
    tableHeaderView.backgroundColor = [UIColor clearColor];
    
    UIView *userView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableHeaderView.width, 60)];
    //头像
    _userImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (userView.height-40)/2, 40, 40)];
    _userImageView.layer.cornerRadius = 5;
    _userImageView.layer.masksToBounds = YES;
    _userImageView.backgroundColor = [UIColor clearColor];
    //昵称
    [_userImageView sd_setImageWithURL:[NSURL URLWithString:_weiboModel.user.profile_image_url]];
    _nickLabel = [[UILabel alloc] initWithFrame:CGRectMake(_userImageView.width+20, (userView.height-40)/2, 100, 40)];
    _nickLabel.font = [UIFont systemFontOfSize:17.0f];
    _nickLabel.backgroundColor = [UIColor clearColor];
    _nickLabel.text = _weiboModel.user.screen_name;
    
    //分割线
    UIImage *lineImage = [UIImage imageNamed:@"userinfo_header_separator"];
    UIImageView *buttomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, userView.height-1,userView.width , 1)];
    buttomLine.image = lineImage;
    
    //右小箭头
    UIImage *detailImage = [UIImage imageNamed:@"icon_detail"];
    UIImageView *detail = [[UIImageView alloc] initWithFrame:CGRectMake(userView.width-30, (userView.height-13)/2,8 , 13)];
    detail.image = detailImage;

    [userView addSubview:_userImageView];
    [userView addSubview:_nickLabel];
    [userView addSubview:buttomLine];
    [userView addSubview:detail];
    
    //微博视图
    float h = [WeiboView getWeiboViewHeight:self.weiboModel isRepost:NO isDetail:YES];
    _weiboView = [[WeiboView alloc] initWithFrame:CGRectMake(10, userView.bottom+10, ScreenWidth-20, h)];
    _weiboView.isDetail = YES;
    _weiboView.weiboModel = _weiboModel;
    
    [tableHeaderView addSubview:userView];
    [tableHeaderView addSubview:_weiboView];
    //tableView头的高度计算，两次相加，一次是用户信息的高度，一次是微博视图的高度
    tableHeaderView.height += userView.height+h+30;
    
    self.tableView.tableHeaderView = tableHeaderView;
    self.tableView.eventDelegate = self;
}

#pragma mark - load data

-(void)loadData{
    NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"SinaWeiboAuthData"][@"accessTokenKey"];
    NSString *weiboId = [_weiboModel.weiboId stringValue];
    if(weiboId.length == 0){
        return ;
    }
    NSLog(@"%@",accessToken);
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:weiboId forKey:@"id"];
    [WBHttpRequest requestWithAccessToken:accessToken url:@"https://api.weibo.com/2/comments/show.json" httpMethod:@"GET" params:params delegate:self withTag:@"loadData"];
}


#pragma mark - weibo delegate

- (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error{
    NSLog(@"网络加载失败：%@",error);
}


- (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result{
    NSError *error;
    NSData *dataContent = [result dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *resultData = [NSJSONSerialization JSONObjectWithData:dataContent options:NSJSONReadingMutableLeaves error:&error];
    NSArray *comments = [resultData objectForKey:@"comments"];
    NSMutableArray *commentsArray = [NSMutableArray arrayWithCapacity:comments.count];
    for (NSDictionary *modelDic in comments) {
        CommentModel * model = [[CommentModel alloc] initWithDataDic:modelDic];
        [commentsArray addObject:model];
    }
    
    if(comments.count >= 20){
        self.tableView.isMore = YES;
    }else{
        self.tableView.isMore = NO;
    }
    
    self.tableView.data = commentsArray;
    self.tableView.commentDic = resultData;
    [self.tableView reloadData];
}

#pragma mark - BaseTableView delegate
//下拉
-(void)pullDown:(BaseTableView *)tableView{
    [tableView performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:2];
}
//上拉
-(void)pullUp:(BaseTableView *)tableView{

}
//选中一个cell
-(void)tableView:(BaseTableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
